package com.fallsvc.chatroom.controller;

import com.fallsvc.chatroom.common.Constants;
import com.fallsvc.chatroom.mapper.MessageMapper;
import com.fallsvc.chatroom.mapper.MessageSessionMapper;
import com.fallsvc.chatroom.model.Friend;
import com.fallsvc.chatroom.model.MessageSession;
import com.fallsvc.chatroom.model.MessageSessionUserItem;
import com.fallsvc.chatroom.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @auther falls_vc
 * description:
 * @date 2025/4/28  10:06
 */
@Slf4j
@RestController
public class MessageSessionController {
    @Resource
    private MessageSessionMapper messageSessionMapper;
    @Resource
    private MessageMapper messageMapper;

    @GetMapping("/sessionList")
    public Object getMessageSessionList(HttpServletRequest request){
        List<MessageSession> messageSessionList=new ArrayList<>();
        // 1. 获取session对象 获得user对象
        HttpSession session=request.getSession(false);
        if(session==null){
            log.info("[getMessageSessionList] session=null");
            return messageSessionList;
        }
        User user =(User) session.getAttribute(Constants.USER);
        if(user==null){
            log.info("[getMessageSessionList] user=null");
            return messageSessionList;
        }
        // 2. 查询会话
        List<Integer> sessionIdList=messageSessionMapper.getSessionIdsByUserId(user.getUserId());
        for(int sessionId:sessionIdList){
            MessageSession messageSession=new MessageSession();
            messageSession.setSessionId(sessionId);
            // 3. 根据sessionId 查找friends会话好友
            List<Friend> friends=messageSessionMapper.getFriendsBySessionId(sessionId,user.getUserId());
            messageSession.setFriends(friends);

            // 4. 查询每个会话最后一条信息
            String lastMessage=messageMapper.getLastMessageBySessionId(sessionId);
            if(lastMessage!=null){
                messageSession.setLastMessage(lastMessage);
            }else{
                messageSession.setLastMessage("");
            }
            messageSessionList.add(messageSession);
        }
        log.info("[getMessageSessionList] 查询会话成功  messageSessionList：{}",messageSessionList);
        return messageSessionList;
    }

    @PostMapping("/session")
    @Transactional
    public Object addMessageSession(int toUserId, @SessionAttribute(Constants.USER)User user){
        HashMap<String,Integer> hashMap=new HashMap<>();
        //1. 创建新会话 获得会话id
        MessageSession messageSession=new MessageSession();
        messageSessionMapper.addMessageSession(messageSession);

        //2. 插入message_session_user 会话和用户id 记录1  登录用户
        MessageSessionUserItem item1=new MessageSessionUserItem();
        item1.setSessionId(messageSession.getSessionId());
        item1.setUserId(user.getUserId());
        messageSessionMapper.addMessageSessionUser(item1);

        //3. 插入message_session_user 会话和用户id 记录2  好友用户
        MessageSessionUserItem item2=new MessageSessionUserItem();
        item2.setSessionId(messageSession.getSessionId());
        item2.setUserId(toUserId);
        messageSessionMapper.addMessageSessionUser(item2);


        // 返回sessionId
        hashMap.put("sessionId",messageSession.getSessionId());
        log.info("[addMessageSession] 新增会话成功{ } ,MessageSessionUserItem1:{},MessageSessionUserItem2:{}",hashMap,item1,item2);
        return hashMap;
    }
}
